<?php
namespace app\api\controller;
header("Content-Type: text/html;charset=utf-8");
header('Access-Control-Allow-Origin:*');
use think\Controller;
use think\Db;
class Add extends Controller
{
  public function file(){
    $uid=input('uid');
    $fname=input('fname');
    $file=request()->file('fpic');
    if ($file){
      $info = $file->validate(['size'=>3000000,'ext'=>'jpg,png,gif'])
      ->rule('uniqid')->move(ROOT_PATH.'public'.DS. 'static'.DS.'Fileimages');
      if($info){
        $date=[
                'uid'=>$uid,
                'fname'=>$fname,
                'fupdatetime'=>date('Y-m-d H:i:s'),
                'fpic'=>$info->getFilename()];
        $addpic=Db::name('file_ck')->insert($date);
        if($addpic)
          return json([
                        'status'=>'1',
                        'msg'=>'保存成功',]);
        else
          return json([
                        'error'=>'0',
                        'msg'=>'保存失败',]);
      }else 
        return json([
                      'status'=>'0',
                      'msg'=>$file->getError()]);
    }else{
          return json([
                        'status'=>'2',
                        'msg'=>$file->getError()]);
    }
  }
  public function collect(){
    $uid=input('uid');
    $tid=input('tid');
    $target=Db::name('template_ck')->where('tid',$tid)->find();
    $status=Db::name('collecion_ck')->where('uid',$uid)->where('tid',$tid)->find();
    if($status){
      return json([
                    'error'=>'0',
                    'msg'=>'已经收藏过了']);
    }else{
    $date=[
            'uid'=>$uid,
            'tid'=>$tid];
    $addcollect=Db::name('collecion_ck')->insert($date);
    if($addcollect){
      $date=[
              'tcollectnum'=>$target['tcollectnum']+1];
      Db::name('template_ck')->where('tid',$tid)->update($date);
      return json([
                    'status'=>'1',
                    'msg'=>'收藏成功']);
    }else
      return json([
                    'status'=>'0',
                    'msg'=>'收藏失败']);
    }
  }

  public function order(){
    $uid=input('uid');
    $sid=input('sid');
    $fid=input('fid');
    $opay=input('opay');
    $osum=input('osum');
    $oendtime=input('oendtime');
    $ostarttime=input('ostarttime');
    $ono=date('Ymd').str_pad(mt_rand(1, 99999),5,'0',STR_PAD_LEFT);

    $target=Db::name('file_ck')->where('uid',$uid)->where('fid',$fid)->find();
    $filePath=ROOT_PATH.'public'.DS. 'static'.DS.'Fileimages'.DS.$target['fpic'];
    $orderPath=ROOT_PATH.'public'.DS. 'static'.DS.'Orderimages'.DS.$target['fpic'];
    $moveFile=copy($filePath,$orderPath);
    $date=[
            'ono'=>$ono,
            'uid'=>$uid,
            'sid'=>$sid,
            'opay'=>$opay,
            'oupdatetime'=>date('Y-m-d H:i:s'),
            'ostate'=>0,
            'oplaystate'=>0,
            'opaystate'=>0,
            'fid'=>$fid,
            'opic'=>$target['fpic'],
            'ostarttime'=>$ostarttime,
            'oendtime'=>$oendtime,
            'osum'=>$osum];
    if($moveFile){
    $addorder=Db::name('order_ck')->insert($date);

    if($addorder)
        return json([
                      'status'=>'1',
                      'msg'=>'下单成功']);
    else
        return json([
                      'status'=>'0',
                      'msg'=>'下单失败']);
  }else{
    return "移动失败";
  }
}

}